package com.zcg.gasmanager.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.google.gson.JsonObject;
import com.zcg.gasmanager.dao.LogDao;
import com.zcg.gasmanager.dao.PayDao;
import com.zcg.gasmanager.dao.RepairDao;
import com.zcg.gasmanager.dao.impl.LogDaoImpl;
import com.zcg.gasmanager.dao.impl.PayDaoImpl;
import com.zcg.gasmanager.dao.impl.RepairDaoImpl;
import com.zcg.gasmanager.model.Log;
import com.zcg.gasmanager.model.Repair;
import com.zcg.gasmanager.model.User;
import com.zcg.gasmanager.service.PayService;
import com.zcg.gasmanager.service.RepairService;
import com.zcg.gasmanager.utils.JsonUtil;
import com.zcg.gasmanager.utils.VeDate;

@Component("repairService")
public class RepairServiceImpl implements RepairService {
	
	private RepairDao repairDao;
	private LogDao logDao;
	private Log log = new Log();
	
	/* (non-Javadoc)
	 * @see com.zcg.gasmanager.service.impl.PayService#update(com.zcg.gasmanager.model.User)
	 */
	@Override
	@Transactional
	public void add(Repair repair){
		repair.setHitchDate(new Date());
		repairDao.add(repair);
		log.setMsg(VeDate.getStringDate()+": add repair");
		logDao.add(log);
	}
	
	/* (non-Javadoc)
	 * @see com.zcg.gasmanager.service.impl.PayService#queryAll()
	 */
	@Override
	@Transactional
	public List<Repair> queryAll(){
		List<Repair> repairs = repairDao.queryAll();
		for(Repair repair:repairs){
			System.out.println(repair.getHitchDate().toLocaleString());
//			repair.setHitchDate(VeDate.strToDateLong());
		}
		log.setMsg(VeDate.getStringDate()+": queryAll repair");
		logDao.add(log);
		return repairs;
	}

	public RepairDao getRepairDao() {
		return repairDao;
	}
	@Resource
	public void setRepairDao(RepairDao repairDao) {
		this.repairDao = repairDao;
	}

	public LogDao getLogDao() {
		return logDao;
	}
	@Resource
	public void setLogDao(LogDao logDao) {
		this.logDao = logDao;
	}

	@Override
	@Transactional
	public String queryJsonLimit(int start, int limit) {
		List<Repair> repairs = repairDao.queryLimit(start, limit);
		log.setMsg(VeDate.getStringDate()+": query repair limit");
		logDao.add(log);
		JsonObject jsonObject = new JsonObject();
		jsonObject.add("root", JsonUtil.toGsonTree(repairs));
		jsonObject.addProperty("totalProperty", repairDao.getCount());
		String result = jsonObject.toString();
		return result;
	}
}
